#include "rtorrent.h"
#include <QMessageBox>

RTorrent::RTorrent(QObject *parent,QString hash)
	: RITorrent(parent,hash)
{
	timer.stop();
}

RTorrent::~RTorrent()
{}

void RTorrent::updateState(){
	get_info("d.get_state");
	get_info("d.is_active");
	get_info("d.is_open");
	get_info("d.is_hash_checking");
}

QList<QString> RTorrent::getParamsList()
{
	QList<QString> tmp;
	tmp << "d.get_hash";
	tmp << "d.get_name";
	tmp << "d.get_size_chunks";
	tmp << "d.get_chunk_size";
	tmp << "d.get_state";
	tmp << "d.is_active";
	tmp << "d.is_open";
	tmp << "d.is_hash_checking";
	tmp << "d.get_completed_chunks";
	tmp << "d.get_down_rate";
	tmp << "d.get_up_rate";
	tmp << "d.get_ratio";
	tmp << "d.get_message";
	tmp << "d.get_priority";
	tmp << "d.get_size_bytes";
	return tmp;
}

void RTorrent::start(bool){
	command("d.start");
	updateState();
}
void RTorrent::pause(bool){
	command("d.pause");
}
void RTorrent::resume(bool){
	command("d.resume");
}
void RTorrent::stop(bool){
	command("d.stop");
	updateState();
}
void RTorrent::check_hash(bool){
	command("d.check_hash");
	updateState();
}
void RTorrent::erase(bool){
	if(QMessageBox::question(0,tr("Erase"),tr("Torrent %1 will be erased. Are you sure?").arg(getName()),QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes)
	{
		command("d.erase");
		timer.stop();
	}
}
void RTorrent::close(bool){
	command("d.close");
	updateState();
}

void RTorrent::get_info(QString param){
	RITorrent::get_info(param,getId());
}

void RTorrent::command(QString param){
	RITorrent::command(param,getId());
}
void RTorrent::update()
{}

void RTorrent::fullUpdate()
{}

QString RTorrent::getName(){
	return params["d.get_name"].toString();
}
double RTorrent::getDownRate(){
	return params["d.get_down_rate"].toDouble();
}
double RTorrent::getUpRate(){
	return params["d.get_up_rate"].toDouble();
}
long RTorrent::getChunkSize(){
	return params["d.get_chunk_size"].toInt();
}
long RTorrent::getCompletedChunks(){
	return params["d.get_completed_chunks"].toInt();
}
long RTorrent::getSizeChunks(){
	return params["d.get_size_chunks"].toInt();
}
int RTorrent::getState(){
	return params["d.get_state"].toInt();
}
bool RTorrent::isHashChecking(){
	return params["d.is_hash_checking"].toBool();
}
double RTorrent::getRatio(){
	return params["d.get_ratio"].toDouble();
}
bool RTorrent::isActive(){
	return params["d.is_active"].toBool();
}
bool RTorrent::isOpen(){
	return params["d.is_open"].toBool();
}
QString RTorrent::getMessage(){
	return params["d.get_message"].toString();
}
int RTorrent::getPriority(){
	return params["d.get_priority"].toInt();
}
void RTorrent::setPriority(int priority){
	RITorrent::command("d.set_priority",getId(),priority);
}
long RTorrent::getSizeBytes(){
	return params["d.get_size_bytes"].toInt();
}

QString RTorrent::getDirectory(){
	return params["d.get_directory"].toString();
}
void RTorrent::setDirectory(QString path){
	RITorrent::command("d.set_directory",getId(),path);
	get_info("d.get_directory");
}

void RTorrent::done(QString method, QString id, QVariant res )
{
	if(method == "d.multicall")
	{
		QList<QVariant> torrents = res.toList();
		QVariant torrent;

		foreach(torrent, torrents)
			if(torrent.toStringList().at(0) == getId()) break;
		
		QStringList value = torrent.toStringList();
		QList<QString> paramlist = getParamsList();

		foreach(QString param,paramlist)
		{
			params[param] = value[paramlist.lastIndexOf(param)];
		}

		emit changeName(getName());
		emit changeSizeChunks(getSizeChunks());
		emit changeChunkSize(getChunkSize());
		emit changeDownRate(getDownRate()/1024);
		emit changeUpRate(getUpRate()/1024);
		emit changeCompletedChunks(getCompletedChunks());	
		emit changeRatio(getRatio()/1000);
		emit changeMessage(getMessage());
		emit changePriority(getPriority());
		emit changeSizeBytes(getSizeBytes());
		emit changeState(getState());
	}

	if(getId() != id) return;
	RITorrent::done(method,id,res);
	if(method == "d.erase") emit erased();
}
